package edu.xjdx.os.controller;

import edu.xjdx.os.domain.Administrator;
import edu.xjdx.os.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("admin")
public class AdminisratorController {
    @Autowired
    private AdministratorService administratorService;

    @PostMapping("/login")
    public String login(String administratorName, String administratorPassword, HttpSession session) {
        Administrator administrator = administratorService.findByName(administratorName);
        if (administrator != null) {
            if (administratorPassword.equals(administrator.getAdministratorPassword())) {
                session.setAttribute("CurrentUser", administrator);
//                return "redirect:/customer/list";
                return "redirect:/goods/statistics";
            } else {
                return "back/login";
            }
        } else {
            return "back/login";
        }
    }

    @PostMapping("/register")
    public String register(String administratorName, String administratorPassword) {
        if (administratorService.register(administratorName, administratorPassword) == true) {
            return "back/login";
        } else {
            return "back/register";
        }
    }


    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("CurrentUser");
        return "back/login";
    }


}
